package com.wish.g04.house.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g04.base.entity.PremisesInfo;
import com.wish.g04.base.entity.User;
import com.wish.g04.base.service.IPremisesInfoService;
import com.wish.g04.base.service.IPrivateFacilityService;
import com.wish.g04.base.service.IPublicFacilityService;
import com.wish.g04.base.service.IUserService;
import com.wish.g04.base.vo.RoomInfoVO;
import com.wish.g04.common.core.web.controller.BaseController;
import com.wish.g04.common.core.web.domain.AjaxResult;
import com.wish.g04.common.core.web.page.TableDataInfo;
import com.wish.g04.base.dto.RoomInfoDTO;
import com.wish.g04.house.service.ITbRoomInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wish.g04.common.security.annotation.PreAuthorize;


/**
 * 【请填写功能名称】Controller
 * 
 * @author xuekai
 * @date 2022-05-30
 */
@RestController
@RequestMapping("/roomInfo")
public class TbRoomInfoController extends BaseController
{
    @Autowired
    private ITbRoomInfoService roomInfoService;

    @Autowired
    private IPublicFacilityService iPublicFacilityService;

    @Autowired
    private IPrivateFacilityService iPrivateFacilityService;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IPremisesInfoService iPremisesInfoService;

    @GetMapping("/allRoominfoList")
    public AjaxResult allList(){
        try {
            roomInfoService.updateRoomInfoAllEs();
            return AjaxResult.success("全量处理成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("全量处理失败");
        }
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize(hasPermi = "system:info:list")
    @GetMapping("/list")
    public TableDataInfo list(RoomInfoDTO roomInfoDTO)
    {
        startPage();
        List<RoomInfoVO> roomInfoVOS = roomInfoService.selectRoomInfoList(roomInfoDTO);
        return getDataTable(roomInfoVOS);
    }


    /**
     * 是否推荐房源处理
     * @param roomInfoDTO
     * @return
     */
    @PutMapping("/editRecommended")
    public AjaxResult edit(@RequestBody RoomInfoDTO roomInfoDTO)
    {
        return roomInfoService.editRecommended(roomInfoDTO);
    }

    /**
     * 房态图
     */
    @PutMapping("/roomAvailabilityFigure")
    public TableDataInfo roomAvailabilityFigure(@RequestBody RoomInfoDTO roomInfoDTO){
        startPage();
        return roomInfoService.roomAvailabilityFigure(roomInfoDTO);
    }

    /**
     * 修改发布状态
     * @param roomInfoDTO
     * @return
     */
    @PutMapping("/updatePublishStatus")
    public AjaxResult updatePublishStatus(@RequestBody RoomInfoDTO roomInfoDTO){
        return roomInfoService.updatePublishStatus(roomInfoDTO);
    }

    /**
     * 获取公共配套
     * @return
     */
    @GetMapping("/getPublicFacility")
    public AjaxResult getPublicFacility(){
        return AjaxResult.success(iPublicFacilityService.list());
    }

    /**
     * 获取私有配套
     * @return
     */
    @GetMapping("/getPrivateFacility")
    public AjaxResult getPrivateFacility(){
        return AjaxResult.success(iPrivateFacilityService.list());
    }

    /**
     * 获取管家
     * @return
     */
    @GetMapping("/getManager")
    public AjaxResult  getManager(){
        return AjaxResult.success(iUserService.list());
    }

    /**
     * 添加分散式房源
     * @param roomInfoDTO
     * @return
     */
    @PutMapping("/saveDistributed")
    public AjaxResult saveDistributed(@RequestBody RoomInfoDTO roomInfoDTO){
        //根据楼盘名称查询楼盘id
        QueryWrapper<PremisesInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",roomInfoDTO.getName());
        PremisesInfo premisesInfo = iPremisesInfoService.getOne(queryWrapper);
        if(premisesInfo==null){
            return AjaxResult.error("楼盘不存在");
        }
        roomInfoDTO.setPremisesId(premisesInfo.getId());
        return roomInfoService.saveDistributed(roomInfoDTO);
    }

    /**
     * 添加集中式房源
     * @param roomInfoDTO
     * @return
     */
    @PutMapping("/saveCentralization")
    public AjaxResult saveCentralization(@RequestBody RoomInfoDTO roomInfoDTO){
        //根据楼盘名称查询楼盘id
        QueryWrapper<PremisesInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",roomInfoDTO.getName());
        PremisesInfo premisesInfo = iPremisesInfoService.getOne(queryWrapper);
        if(premisesInfo==null){
            return AjaxResult.error("楼盘不存在");
        }
        roomInfoDTO.setPremisesId(premisesInfo.getId());
        return roomInfoService.saveCentralization(roomInfoDTO);
    }



  /*  *//**
     * 导出【请填写功能名称】列表
     *//*
    @PreAuthorize(hasPermi = "system:info:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbRoomInfo tbRoomInfo) throws IOException
    {
        List<TbRoomInfo> list = tbRoomInfoService.selectTbRoomInfoList(tbRoomInfo);
        ExcelUtil<TbRoomInfo> util = new ExcelUtil<TbRoomInfo>(TbRoomInfo.class);
        util.exportExcel(response, list, "info");
    }

    *//**
     * 获取【请填写功能名称】详细信息
     *//*
    @PreAuthorize(hasPermi = "system:info:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(tbRoomInfoService.selectTbRoomInfoById(id));
    }

    *//**
     * 新增【请填写功能名称】
     *//*
    @PreAuthorize(hasPermi = "system:info:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbRoomInfo tbRoomInfo)
    {
        return toAjax(tbRoomInfoService.insertTbRoomInfo(tbRoomInfo));
    }



    *//**
     * 删除【请填写功能名称】
     *//*
    @PreAuthorize(hasPermi = "system:info:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tbRoomInfoService.deleteTbRoomInfoByIds(ids));
    }*/
}
